package com.pdg.islandinterloper.database.helpers;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.pdg.islandinterloper.R;

import android.app.Activity;

public class NameGenerator {
	
	private static final int DIE_SIZE = 6;
	private static final int MINIMUM_LENGTH = 5;

	public static String generateName(Activity theActivity,Random theRandom){
		StringBuilder theBuilder = new StringBuilder();
		String[] theConsonants = theActivity.getResources().getStringArray(R.array.name_generation_consonants);
		String[] theVowels = theActivity.getResources().getStringArray(R.array.name_generation_vowels);
		int theNameSize = theRandom.nextInt(DIE_SIZE)+theRandom.nextInt(DIE_SIZE)+MINIMUM_LENGTH;
		boolean consonantFlag = theRandom.nextBoolean();
		while(theNameSize>0){
			if(consonantFlag){
				theBuilder.append(theConsonants[theRandom.nextInt(theConsonants.length)]);
			}else{
				theBuilder.append(theVowels[theRandom.nextInt(theVowels.length)]);
			}
			consonantFlag=!consonantFlag;
			theNameSize--;
		}
		return theBuilder.toString();
	}
	public static List<String> generateNames(Activity theActivity,Random theRandom,int theCount){
		List<String> theResult = new ArrayList<String>();
		while(theResult.size()<theCount){
			boolean found = false;
			String theCandidate = generateName(theActivity,theRandom);
			for(String theName:theResult){
				if(theCandidate.equalsIgnoreCase(theName)){
					found = true;
					break;
				}
			}
			if(!found){
				theResult.add(theCandidate);
			}
		}
		return theResult;
	}
}
